﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WPP_Service.Data;

namespace WPP_Service.Test
{
    [TestClass]
    public class ServiceTest
    {

        #region dates1
        [TestMethod]
        //Ensure proper output for date searches
        public void testDates()
        {
            //create an order query     
            OrderQuery testQuery1 = new OrderQuery();
           
            //create start and end dates
            DateTime start = new DateTime(2010, 1, 18);
            DateTime end = new DateTime(2010, 1, 28);
            
            //create array for the dateGroup
            testQuery1.Dates = new DateGroup[1];            
            testQuery1.Dates[0] = new DateGroup();
            
            //populate the query with test data
            testQuery1.Dates[0].StartDate = start;
            testQuery1.Dates[0].EndDate = end;
            testQuery1.Dates[0].DateType = (DateType)2;

            List<string> orderQuery = new List<string>();
            List<string> packageQuery = new List<string>();

            //use the code from WPPService.cs to build a list for the query
            if (testQuery1.Dates != null)
            {

                foreach (DateGroup i in testQuery1.Dates)
                {

                    if ((int)i.DateType == 0)
                    {
                        orderQuery.Add(" o.order_process_date > '" + i.StartDate.ToShortDateString() + "' and o.order_process_date < '" + i.EndDate.ToShortDateString() + "' ");
                    }

                    if ((int)i.DateType == 1)
                    {
                        orderQuery.Add(" o.order_creation_date > '" + i.StartDate.ToShortDateString() + "' and o.order_creation_date < '" + i.EndDate.ToShortDateString() + "' ");
                    }

                    if ((int)i.DateType == 2)
                    {
                        orderQuery.Add(" o.order_transaction_date > '" + i.StartDate.ToShortDateString() + "' and o.order_transaction_date < '" + i.EndDate.ToShortDateString() + "' ");
                    }

                    if ((int)i.DateType == 3)
                    {
                        packageQuery.Add(" p.package_date_boxed > '" + i.StartDate.ToShortDateString() + "' and p.package_date_boxed < '" + i.EndDate.ToShortDateString() + "' and p.Order_id = o.Order_id");
                    }

                    if ((int)i.DateType == 4)
                    {
                        packageQuery.Add(" p.package_pickup_date > '" + i.StartDate.ToShortDateString() + "' and p.package_pickup_date < '" + i.EndDate.ToShortDateString() + "' and p.Order_id = o.Order_id");
                    }
                }
            }
            //use the same code from WPPService.cs to populate the string arrays with the data from the lists
            string[] orderQueryArray = orderQuery.ToArray();
            string[] packageQueryArray = packageQuery.ToArray();
            string queryString = " ";

            for (int i = 0; i < orderQueryArray.Length; i++)
            {
                if (i != 0)
                    queryString += " AND " + orderQueryArray[i];

                else
                    queryString += " " + orderQueryArray[i];

            }

            for (int i = 0; i < packageQueryArray.Length; i++)
            {
                if (i != 0)
                    queryString += " AND " + packageQueryArray[i];

                else
                    queryString += " " + packageQueryArray[i];

            }

            //dased on test data we expect the folowing string
            string expected = "o.order_transaction_date > '1/18/2010' and o.order_transaction_date < '1/28/2010' ";

            bool result = false;
            //after trimming the string test to see if they are equal 
            if (expected.Trim().Equals(queryString.Trim()))
                result = true;

            Assert.IsTrue(result);
        }

#endregion

        #region dates2
        [TestMethod]
        //this method will be used to verify the search for data with multiple date types
        public void MultiDateTest()
        {

            //create an order query   
            OrderQuery testQuery2 = new OrderQuery();
            
            //create two differnt date types and ranges for the order query 
            testQuery2.Complete = false;
            testQuery2.Dates = new DateGroup[2];
            testQuery2.Dates[0] = new DateGroup();
            testQuery2.Dates[1] = new DateGroup();

            //add data to the query
            testQuery2.Dates[0].StartDate = new DateTime(2011, 3, 10);
            testQuery2.Dates[0].EndDate = new DateTime(2013, 3, 10);
            testQuery2.Dates[0].DateType = (DateType)3;
            testQuery2.Dates[1].StartDate = new DateTime(2011, 3, 10);
            testQuery2.Dates[1].EndDate = new DateTime(2013, 3, 10);
            testQuery2.Dates[1].DateType = (DateType)4;

            List<string> orderQuery = new List<string>();
            List<string> packageQuery = new List<string>();

            //use the code from WPPService.cs to build a list for the query
            if (testQuery2.Dates != null)
            {

                foreach (DateGroup i in testQuery2.Dates)
                {

                    if ((int)i.DateType == 0)
                    {
                        orderQuery.Add(" o.order_process_date > '" + i.StartDate.ToShortDateString() + "' and o.order_process_date < '" + i.EndDate.ToShortDateString() + "' ");
                    }

                    if ((int)i.DateType == 1)
                    {
                        orderQuery.Add(" o.order_creation_date > '" + i.StartDate.ToShortDateString() + "' and o.order_creation_date < '" + i.EndDate.ToShortDateString() + "' ");
                    }

                    if ((int)i.DateType == 2)
                    {
                        orderQuery.Add(" o.order_transaction_date > '" + i.StartDate.ToShortDateString() + "' and o.order_transaction_date < '" + i.EndDate.ToShortDateString() + "' ");
                    }

                    if ((int)i.DateType == 3)
                    {
                        packageQuery.Add(" p.package_date_boxed > '" + i.StartDate.ToShortDateString() + "' and p.package_date_boxed < '" + i.EndDate.ToShortDateString() + "' and p.Order_id = o.Order_id");
                    }

                    if ((int)i.DateType == 4)
                    {
                        packageQuery.Add(" p.package_pickup_date > '" + i.StartDate.ToShortDateString() + "' and p.package_pickup_date < '" + i.EndDate.ToShortDateString() + "' and p.Order_id = o.Order_id");
                    }
                }


                if (testQuery2.Complete != null)
                {
                    if (testQuery2.Complete == true)
                        packageQuery.Add(" p.complete = 1");
                    else
                        packageQuery.Add(" p.complete = 0");
                }
            }
            //build the string for the query
            string[] orderQueryArray = orderQuery.ToArray();
            string[] packageQueryArray = packageQuery.ToArray();
            string queryString = " ";

            for (int i = 0; i < orderQueryArray.Length; i++)
            {
                if (i != 0)
                    queryString += " AND " + orderQueryArray[i];

                else
                    queryString += " " + orderQueryArray[i];

            }

            for (int i = 0; i < packageQueryArray.Length; i++)
            {
                if (i != 0)
                    queryString += " AND " + packageQueryArray[i];

                else
                    queryString += " " + packageQueryArray[i];

            }

            //expected outcome for the multi date orderQuery object
            string expected = " p.package_date_boxed > '3/10/2011' and p.package_date_boxed < '3/10/2013' " + 
       "and p.Order_id = o.Order_id AND  p.package_pickup_date > '3/10/2011' and p.package_pickup_date < '3/10/2013' and p.Order_id = o.Order_id " + 
        "AND  p.complete = 0 ";


            bool result = false;
            //ensure the returned string is equal to the expected
            if (expected.Trim().Equals(queryString.Trim()))
                result = true;

            Assert.IsTrue(result);

        }

        #endregion

        #region Other Data

        [TestMethod]
        //Ensure proper output for date searches
        public void testData()
        {
            //create test query
            OrderQuery testQuery3 = new OrderQuery();

            //set data to test
            testQuery3.Boxed = true;
            testQuery3.Multibox = true;
            testQuery3.Complete = false;

          
            List<string> packageQuery = new List<string>();
            string queryString = " ";

            //use code from WPPService.cs to test the statements
            if (testQuery3.Complete != null)
            {
                if (testQuery3.Complete == true)
                    packageQuery.Add(" p.complete = 1");
                else
                    packageQuery.Add(" p.complete = 0");
            }

            if (testQuery3.Multibox != null)
            {
                if (testQuery3.Multibox == true)
                    packageQuery.Add(" p.multi_box = 1");
                else
                    packageQuery.Add(" p.multi_box = 0");
            }

            if (testQuery3.Boxed != null)
            {
                if (testQuery3.Multibox == true)
                    packageQuery.Add(" p.boxed = 1 ");
                else
                    packageQuery.Add(" p.boxed = 0 ");

            }

            //add data to the query string
            string[] packageQueryArray = packageQuery.ToArray();

            for (int i = 0; i < packageQueryArray.Length; i++)
            {
                if (i != 0)
                    queryString += " AND " + packageQueryArray[i];

                else
                    queryString += " " + packageQueryArray[i];

            }

            //expected string based on test data
            string expected = "p.complete = 0 AND  p.multi_box = 1 AND  p.boxed = 1 ";

            bool result = false;
            //test the returned string to see if it equals the expected string
            if (expected.Trim().Equals(queryString.Trim()))
                result = true;

            Assert.IsTrue(result);
        }

        #endregion

    }
}
